const dotenv =  require('dotenv');
const {join}=require('path');
dotenv.config({path:join(__dirname,'../','.env')});
const port = (process.env.APP_PORT||3000);
const  {log}  = require('./log.js')

const express = require('express')
const bodyParser = require('body-parser')
const registerRouter = require('./router/register')
const loginRouter = require('./router/login')
const createMeshRouter = require('./router/create_mesh')
const fileUploadRouter = require('./router/fileUpload')
const imageUploadRouter = require('./router/imageUpload')
const getMeshInfoRouter = require('./router/get_mesh_info')
const sendEmailVerificationCodeRouter = require('./router/send_email_verification_code')
const sendEmailVerificationCodeRouterAws = require('./router/send_email_verification_code_aws')
const verifyAuthRouter = require('./router/verify_auth')
const createBuildingRouter = require('./router/create_building')
const shareQRCodeRouter = require('./router/share_QR_code')
const scanCodeRouter = require('./router/scanCode')
const getVersionRouter = require('./router/get_version')
const deleteMeshRouter = require('./router/delete_building_mesh')
const updateUserInfoMeshRouter = require('./router/update_user_info')
const deleteUserRouter = require('./router/delete_user')
const buildingRouter = require('./router/building')
const meshRouter = require('./router/mesh')
const headRouter = require('./router/head')
const QRReceiverListRouter = require('./router/QR_receiver_list')
const delShareBuildingRouter = require('./router/del_share_building')
const app = express()

app.all('*', function (req, res, next) {
    // 开发环境设置，生产环境谨慎使用
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    if (req.method === 'OPTIONS') {
        res.sendStatus(200);
    } else {
        next();
    }
});

app.get('/', (req, res) => {
    // 假设远程服务器的地址是 http://192.168.1.5:8080/somepath
    const remoteUrl = 'https://iot.hsesmartinc.com/web';
    res.redirect(remoteUrl);
});

app
    .use(bodyParser.json())
    .use(bodyParser.urlencoded({extended: true}))
    .use(express.static('public'))
    .use('/register', registerRouter)
    .use('/login', loginRouter)
    .use('/createMesh', createMeshRouter)
    .use('/fileUpload', fileUploadRouter)
    .use('/imageUpload', imageUploadRouter)
    .use("/getMeshInfo", getMeshInfoRouter)
    .use("/sendEmailVerificationCode", sendEmailVerificationCodeRouter)
    .use("/sendEmailVerificationCodeAws", sendEmailVerificationCodeRouterAws)
    .use("/verifyAuth", verifyAuthRouter)
    .use("/createBuilding", createBuildingRouter)
    .use("/shareqrcode", shareQRCodeRouter)
    .use("/scancode", scanCodeRouter)
    .use("/getversion", getVersionRouter)
    .use("/deletemesh", deleteMeshRouter)
    .use("/updateuserinfo", updateUserInfoMeshRouter)
    .use("/deleteuser", deleteUserRouter)
    .use("/building", buildingRouter)
    .use("/mesh", meshRouter)
    .use("/head", headRouter)
    .use("/qrreceiverlist", QRReceiverListRouter)
    .use("/delsharebuilding", delShareBuildingRouter)
    .listen(port, () => log.info(`HAISEN app listening on port ${port}!`))